﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
namespace BeatingMole
{
    class CBusSettings
    {
        #region "Variable"
        CInfoSettings InfoSettings;
        ContentManager _Content;
        Vector2 PosSettings;
        bool _isEnableBusMenu;
        bool IsReady = false;
        bool IsSoundOn;
        bool IsSoundEffectOn;
        bool IsClick;
        bool IsClickSF;
        bool IsSaveSettings = false;
        float DelaySaveSettings;

        MouseState MouseCur;
        MouseState MousePre;

        MouseState MouseCurSF;
        MouseState MousePreSF;

        MouseState MouseCurSave;
        MouseState MousePreSave;

        MouseState MouseCurGoBackMenu;
        MouseState MousePreGoBackMenu;

        float BarVolume = 200;
        float BarBrightness = 200;
        float BarSoundEffect = 200;
        float ChiSo = 0.005f;
        #endregion
        #region Save Settings
        IsolatedStorageSettings IsoVolumne = IsolatedStorageSettings.ApplicationSettings;
        IsolatedStorageSettings IsoVolumneSoundEffect = IsolatedStorageSettings.ApplicationSettings;
        IsolatedStorageSettings IsoBrightness = IsolatedStorageSettings.ApplicationSettings;
        IsolatedStorageSettings IsoCheckSound = IsolatedStorageSettings.ApplicationSettings;
        IsolatedStorageSettings IsoCheckSoundEffect = IsolatedStorageSettings.ApplicationSettings;
        #endregion
        #region "Properties"
        public bool IsEnableBusMenu
        {
            set { _isEnableBusMenu = value; }
            get { return _isEnableBusMenu; }
        }
        #endregion
        #region "Constructor"
        public CBusSettings(ContentManager content)
        {
            _Content = content;
            InfoSettings = new CInfoSettings(_Content);
            PosSettings = new Vector2(-800, 0);
            //Load Thông Tin Settings Trước Đó
            LoadSettings();
            if (IsSoundOn == true)
            {
                IsClick = true;
                MediaPlayer.Volume = 0;
            }
            else
            {
                SetVolume(BarVolume);
            }
            if (IsSoundEffectOn == true)
            {
                IsClickSF = true;
                SoundEffect.MasterVolume = 0;
            }
            else
            {
                SetSoundEffect(BarSoundEffect);
            }
            SetBrightness(BarBrightness);
        }
        #endregion
        #region "Method"
        /// <summary>
        /// Hàm cập nhật của Settings
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            ManagerSettings();
            GoBackMenu();
            DelaySaveSettings += (float)gameTime.ElapsedGameTime.Milliseconds;
            if (DelaySaveSettings >= 3000)
            {
                DelaySaveSettings = 0;
                IsSaveSettings = false;
            }
            if (IsReady == true)
            {
                ClickButtonOnOff();
                ClickButtonSoundEffectOnOff(); 
                ClickPlusOrExceptBrightness();
                ClickPlusOrExcept();
                ClickPlusOrExceptSoundEffect();
                SaveSettings();
            }
        }
        /// <summary>
        /// Hàm điều chỉnh âm thanh
        /// </summary>
        /// <param name="Value">Giá trị âm thanh</param>
        public void SetVolume(float Value)
        {
            MediaPlayer.Volume = Value * ChiSo;
        }
        /// <summary>
        /// Hàm điều chỉnh độ sáng
        /// </summary>
        /// <param name="Value"></param>
        public void SetBrightness(float Value)
        {
           //Đang Hoàn thiện ...
        }
        /// <summary>
        /// Hàm điều chỉnh âm lượng hiệu ứng
        /// </summary>
        /// <param name="Value"></param>
        public void SetSoundEffect(float Value)
        {
            SoundEffect.MasterVolume = Value * ChiSo;
        }
        //Slider Âm Lượng
        public void ClickPlusOrExcept()
        {
            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoSettings.RecPlus))
                {
                    BarVolume++;
                    if (BarVolume >= 200)
                    {
                        BarVolume = 200;
                    }
                    SetVolume(BarVolume);
                }
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoSettings.RecExcept))
                {
                    BarVolume--;
                    if (BarVolume <= 0)
                    {
                        BarVolume = 0;
                    }
                    SetVolume(BarVolume);
                }
                
            }
        }
        /// <summary>
        /// Slider Độ Sáng
        /// </summary>
        public void ClickPlusOrExceptBrightness()
        {
            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoSettings.RecPlusB))
                {
                    BarBrightness++;
                    if (BarBrightness >= 200)
                    {
                        BarBrightness = 200;
                    }
                    SetBrightness(BarBrightness);
                }
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoSettings.RecExceptB))
                {
                    BarBrightness--;
                    if (BarBrightness <= 0)
                    {
                        BarBrightness = 0;
                    }
                    SetBrightness(BarBrightness);
                }
                
            }
        }
        /// <summary>
        /// Slider Hiệu Ứng Âm Thanh
        /// </summary>
        public void ClickPlusOrExceptSoundEffect()
        {
            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoSettings.RecPlusSoundEffect))
                {
                    BarSoundEffect++;
                    if (BarSoundEffect >= 200)
                    {
                        BarSoundEffect = 200;    
                    }
                    SetSoundEffect(BarSoundEffect);
                    
                }
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoSettings.RecExceptSoundEffect))
                {
                    BarSoundEffect--;
                    if (BarSoundEffect <= 0)
                    {
                        BarSoundEffect = 0;  
                    }
                    SetSoundEffect(BarSoundEffect);   
                }
            }
        }
        /// <summary>
        /// Tắt/bật âm thanh
        /// </summary>
        public void ClickButtonOnOff()
        {
            MouseCur = Mouse.GetState();
            if (MouseCur.LeftButton == ButtonState.Pressed && MousePre.LeftButton == ButtonState.Released)
            {
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoSettings.RecButtonOn))
                {
                    if (IsClick == true)
                    {
                        IsSoundOn = false;
                        IsClick = false; 
                        SetVolume(BarVolume);
                    }
                    else
                    {
                        IsSoundOn = true;
                        IsClick = true;
                        MediaPlayer.Volume = 0;
                    }
                }
            }
            MousePre = MouseCur;
        }
        /// <summary>
        /// Tắt/bật âm lượng
        /// </summary>
        public void ClickButtonSoundEffectOnOff()
        {
            MouseCurSF = Mouse.GetState();
            if (MouseCurSF.LeftButton == ButtonState.Pressed && MousePreSF.LeftButton == ButtonState.Released)
            {
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoSettings.RecButtonSoundEffectOn))
                {
                    if (IsClickSF == true)
                    {
                        IsSoundEffectOn = false;
                        IsClickSF = false;
                        SetSoundEffect(BarSoundEffect);
                    }
                    else
                    {
                        IsSoundEffectOn = true;
                        IsClickSF = true;
                        SoundEffect.MasterVolume = 0;
                    }
                }
            }
            MousePreSF = MouseCurSF;
        }

        private void LoadSettings()
        {
            //Load Volumne
            if (IsoVolumne.Contains("AnhDuySettings_IsoVolumne"))
            {
                BarVolume = (float)IsoVolumne["AnhDuySettings_IsoVolumne"];
            }
            //Load VolumeSoundEffect
            if (IsoVolumneSoundEffect.Contains("AnhDuySettings_IsoVolumneSoundEffect"))
            {
                BarSoundEffect = (float)IsoVolumneSoundEffect["AnhDuySettings_IsoVolumneSoundEffect"];
            }
            //Load Bightness
            if (IsoBrightness.Contains("AnhDuySettings_IsoBrightness"))
            {
                BarBrightness = (float)IsoBrightness["AnhDuySettings_IsoBrightness"];
            }
            //Load Check Sound
            if (IsoCheckSound.Contains("AnhDuySettings_IsoCheckSound"))
            {
                IsSoundOn = (bool)IsoCheckSound["AnhDuySettings_IsoCheckSound"];
            }
            //Save Check  Sound Effect
            if (IsoCheckSoundEffect.Contains("AnhDuySettings_IsoCheckSoundEffect"))
            {
                IsSoundEffectOn = (bool)IsoCheckSoundEffect["AnhDuySettings_IsoCheckSoundEffect"];
            }
            //IsSoundOn = true;
            //IsSoundEffectOn = true;
        }

        private void SaveSettings()
        {
            MouseCurSave = Mouse.GetState();
            if (MouseCurSave.LeftButton == ButtonState.Pressed && MousePreSave.LeftButton == ButtonState.Released)
            {
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoSettings.RecBtnSaveSettings))
                {
                    //Save Volumne
                    if (IsoVolumne.Contains("AnhDuySettings_IsoVolumne"))
                    {
                        IsoVolumne["AnhDuySettings_IsoVolumne"] = BarVolume;
                    }
                    else
                    {
                        IsoVolumne.Add("AnhDuySettings_IsoVolumne", BarVolume);
                    }
                    IsoVolumne.Save();
                    //Save VolumeSoundEffect
                    if (IsoVolumneSoundEffect.Contains("AnhDuySettings_IsoVolumneSoundEffect"))
                    {
                        IsoVolumneSoundEffect["AnhDuySettings_IsoVolumneSoundEffect"] = BarSoundEffect;
                    }
                    else
                    {
                        IsoVolumneSoundEffect.Add("AnhDuySettings_IsoVolumneSoundEffect", BarSoundEffect);
                    }
                    IsoVolumneSoundEffect.Save();
                    //Save Bightness
                    if (IsoBrightness.Contains("AnhDuySettings_IsoBrightness"))
                    {
                        IsoBrightness["AnhDuySettings_IsoBrightness"] = BarBrightness;
                    }
                    else
                    {
                        IsoBrightness.Add("AnhDuySettings_IsoBrightness", BarBrightness);
                    }
                    IsoBrightness.Save();
                    //Save Check Sound
                    if (IsoCheckSound.Contains("AnhDuySettings_IsoCheckSound"))
                    {
                        IsoCheckSound["AnhDuySettings_IsoCheckSound"] = IsSoundOn;
                    }
                    else
                    {
                        IsoCheckSound.Add("AnhDuySettings_IsoCheckSound", IsSoundOn);
                    }
                    IsoCheckSound.Save();
                    //Save Check  Sound Effect
                    if (IsoCheckSoundEffect.Contains("AnhDuySettings_IsoCheckSoundEffect"))
                    {
                        IsoCheckSoundEffect["AnhDuySettings_IsoCheckSoundEffect"] = IsSoundEffectOn;
                    }
                    else
                    {
                        IsoCheckSoundEffect.Add("AnhDuySettings_IsoCheckSoundEffect", IsSoundEffectOn);
                    }
                    IsoCheckSoundEffect.Save();
                    IsSaveSettings = true;
                }
            }
            MousePreSave = MouseCurSave;
        }

        private void GoBackMenu()
        {
            MouseCurGoBackMenu = Mouse.GetState();
            if (MouseCurGoBackMenu.LeftButton == ButtonState.Pressed && MousePreGoBackMenu.LeftButton == ButtonState.Released)
            {
                if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoSettings.RecBtnBackMenu))
                {
                    CGlobalVariable.PosMenuBack = CGlobalVariable.PositionMenu.RightBack;
                    IsEnableBusMenu = true;
                    IsReady = false;
                    CGlobalVariable.IsEnableMenu = true;
                }
            }
            MousePreGoBackMenu = MouseCurGoBackMenu;
        }

        /// <summary>
        /// Quản lý lên xuống
        /// </summary>
        private void ManagerSettings()
        {
            if (IsEnableBusMenu == false)
            {
                PosSettings.X += CGlobalVariable.SpeedTranslate;
                if (PosSettings.X >= 0)
                {
                    PosSettings.X = 0;
                    IsReady = true;
                }
            }
            else
            {
                PosSettings.X -= CGlobalVariable.SpeedTranslate;
                if (PosSettings.X <= -800)
                {
                    PosSettings.X = -800;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(InfoSettings.BgSettings, PosSettings, Color.White);
            spriteBatch.Draw(InfoSettings.BlackBacground, PosSettings, Color.Black * CGlobalVariable.Opacity);
            //Draw Button Save Settings
            spriteBatch.Draw(InfoSettings.BtnSaveSettings, new Vector2(550 + PosSettings.X, 20), Color.White);
            spriteBatch.Draw(InfoSettings.BtnBackMenu, new Vector2(625 + PosSettings.X, 20), Color.White);
            //Draw Text
            spriteBatch.DrawString(InfoSettings.SpriteFontSettings, "BẬT/TẮT ÂM THANH : ", new Vector2(50 + PosSettings.X,125), Color.White);
            spriteBatch.DrawString(InfoSettings.SpriteFontSettings, "ÂM THANH : ", new Vector2(50 + PosSettings.X, 190), Color.White);
            spriteBatch.DrawString(InfoSettings.SpriteFontSettings, "ĐỘ SÁNG : ", new Vector2(50 + PosSettings.X, 270), Color.White);
            spriteBatch.DrawString(InfoSettings.SpriteFontSettings, "BẬT/TẮT HIỆU ỨNG ÂM THANH : ", new Vector2(50 + PosSettings.X, 340), Color.White);
            spriteBatch.DrawString(InfoSettings.SpriteFontSettings, "HIỆU ỨNG ÂM THANH : ", new Vector2(50 + PosSettings.X, 420), Color.White);
            //Volume
            spriteBatch.Draw(InfoSettings.Except, new Vector2(375 + PosSettings.X, 175), Color.White);
            spriteBatch.Draw(InfoSettings.BarVolume, new Vector2(440 + PosSettings.X, 182), new Rectangle((int)(390 + PosSettings.X), 182, (int)BarVolume, 48), Color.White);
            spriteBatch.Draw(InfoSettings.Plus, new Vector2(640 + PosSettings.X, 175), Color.White);
            //Volume


            //Brightness
            spriteBatch.Draw(InfoSettings.ExceptB, new Vector2(375 + PosSettings.X, 250), Color.White);
            spriteBatch.Draw(InfoSettings.BarBrightness, new Vector2(440 + PosSettings.X, 257), new Rectangle((int)(390 + PosSettings.X), 225, (int)BarBrightness, 48), Color.White);
            spriteBatch.Draw(InfoSettings.PlusB, new Vector2(640 + PosSettings.X, 250), Color.White); 
            //Brightness


            //Draw Slider Sound Effect
            spriteBatch.Draw(InfoSettings.ExceptSoundEffect, new Vector2(375 + PosSettings.X, 400), Color.White);
            spriteBatch.Draw(InfoSettings.BarSoundEffect, new Vector2(440 + PosSettings.X, 407), new Rectangle((int)(390 + PosSettings.X), 225, (int)BarSoundEffect, 48), Color.White);
            spriteBatch.Draw(InfoSettings.PlusSoundEffect, new Vector2(640 + PosSettings.X, 400), Color.White); 


            //Draw Button
            if (IsSoundOn == false)
            {
                spriteBatch.Draw(InfoSettings.ButtonSettings, new Vector2(375 + PosSettings.X, 105), InfoSettings.XmlButtonSettings["no"], Color.White);
            }
            else
            {
                spriteBatch.Draw(InfoSettings.ButtonSettings, new Vector2(375 + PosSettings.X, 105), InfoSettings.XmlButtonSettings["yes"], Color.White);
            }

            //Draw Button Sound Effect
            if (IsSoundEffectOn == false)
            {
                spriteBatch.Draw(InfoSettings.ButtonSettings, new Vector2(450 + PosSettings.X, 330), InfoSettings.XmlButtonSettings["no"], Color.White);
            }
            else
            {
                spriteBatch.Draw(InfoSettings.ButtonSettings, new Vector2(450 + PosSettings.X, 330), InfoSettings.XmlButtonSettings["yes"], Color.White);
            }

            if (IsSaveSettings == true)
            {
                spriteBatch.Draw(InfoSettings.BlackBacground, Vector2.Zero, Color.Black * 0.75f);
                spriteBatch.Draw(InfoSettings.BgSaveSettings, new Vector2(200, 125), Color.White);
            }
        }
        #endregion
    }
}
